package com.bilibili.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bilibili.dao.AdminisDao;
import com.bilibili.dao.BaseDao;
import com.bilibili.entity.User;

public class AdminisDaoImpl implements AdminisDao{

	@Override
	public User selectUser(String username, String password) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		User user = null;
		String sql = "SELECT * FROM user WHERE username = ? AND password = ? AND role = 1";
		try {
			con = BaseDao.getConn();
			ps = con.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			System.out.println(ps);
			res = ps.executeQuery();
			if(res.next()) {
				user = new User();
				user.setUserId(res.getInt("user_id"));
				user.setUsername(res.getString("username"));
				user.setPassword(res.getString("password"));
				user.setNickname(res.getString("nickname"));
				user.setGender(res.getInt("gender"));
				user.setBirthday(res.getDate("birthday"));
				user.setRole(res.getInt("role"));
				user.setAvatarUrl(res.getString("avatar_url"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return user;
	}
	
}
